/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年6月26日
 * @version 0.1
 */
package com.suredy.base.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.suredy.tools.SuredyTreeModel;


/**
 * @author ZhangMaoren
 *
 */
@JsonInclude(Include.NON_EMPTY)
public class MenuTreeModel extends SuredyTreeModel {


	/**
	 * 
	 */
	private static final long serialVersionUID = -5049497180607174329L;
	
	private Menu data;

	public Menu getData() {
		return data;
	}

	public void setData(Menu data) {
		this.data = data;
	}

	public static List<MenuTreeModel> makeTree(List<Menu> menus) {
		if (menus == null || menus.isEmpty()) {
			return null;
		}

		List<MenuTreeModel> ret = new ArrayList<MenuTreeModel>();

		for (Menu menu : menus) {
			MenuTreeModel node = makeOneNode(menu);

			if (node != null) {
				ret.add(node);
			}
		}

		return ret.isEmpty() ? null : ret;
	}
	
	public static List<MenuTreeModel> makeTree(List<Menu> menus, List<String> allowedMenues) {
		if (menus == null || menus.isEmpty()) {
			return null;
		}

		List<MenuTreeModel> ret = new ArrayList<MenuTreeModel>();

		for (Menu menu : menus) {
			MenuTreeModel node = makeOneNode(menu, allowedMenues);

			if (node != null) {
				ret.add(node);
			}
		}

		return ret.isEmpty() ? null : ret;
	}
	
	private static MenuTreeModel makeOneNode(Menu menu, List<String> allowedMenues) {
		if (menu == null) {
			return null;
		}
		
		if ( (StringUtils.isBlank(menu.getResourceId())) || 
				(!StringUtils.isBlank(menu.getResourceId()) && allowedMenues.contains(menu.getResourceId())) ) {
			//菜单未关联安全节点通过，关联安全节点且准许访问通过
			MenuTreeModel node = new MenuTreeModel();
			node.setId(menu.getId());
			node.setText(menu.getText());
			node.setCollapse(false);
			
			if (menu.getChildren() != null && !menu.getChildren().isEmpty()) {
				node.setChildren(new ArrayList<SuredyTreeModel>());
				for (Menu child : menu.getChildren()) {
					MenuTreeModel cNode = makeOneNode(child, allowedMenues);

					if (cNode != null) {
						node.getChildren().add(cNode);
					}
				}
				if (node.getChildren() == null || node.getChildren().isEmpty()) {
					node.setLeaf(true);
				} else {
					node.setLeaf(false);
				}
				
			} else {
				node.setLeaf(true);
			}

			menu.setChildren(null);
			node.setData(menu);

			return node;
			
		} else {
			return null;
		}		

	}
	
	private static MenuTreeModel makeOneNode(Menu menu) {
		if (menu == null) {
			return null;
		}

		MenuTreeModel node = new MenuTreeModel();
		node.setId(menu.getId());
		node.setText(menu.getText());
		node.setCollapse(false);
		

		if (menu.getChildren() != null && !menu.getChildren().isEmpty()) {
			node.setLeaf(false);
			node.setChildren(new ArrayList<SuredyTreeModel>());

			for (Menu child : menu.getChildren()) {
				MenuTreeModel cNode = makeOneNode(child);

				if (cNode != null) {
					node.getChildren().add(cNode);
				}
			}
		} else {
			node.setLeaf(true);
		}

		menu.setChildren(null);
		node.setData(menu);

		return node;
	}


	public static MenuTreeModel makeNode(Menu menu) {
		return makeOneNode(menu);
	}


	
}
